/**************************************************
 * FreeESP by compudaze (compudaze@gmail.com)
 * main.cpp
 **************************************************/

#include "main.h"
#include "globals.h"
#include "norecoil.h"
#include "esp.h"
#include "thermal.h"
#include "forcehost.h"

DWORD WINAPI LoopFunction(LPVOID lpParam)
{
	while (true) {
		if (GetAsyncKeyState(VK_F5) & 1) {
			forceHostEnable();
		}
		else if (GetAsyncKeyState(VK_F6) & 1) {
			forceHostDisable();
		}
		if (GetAsyncKeyState(VK_F7) & 1) {
			setMinPlayers();
		}
		else if (GetAsyncKeyState(VK_F8) & 1) {
			drawFpsToggle();
		}
		else if (GetAsyncKeyState(VK_F9) & 1) {
			noRecoilToggle();
		}
		else if (GetAsyncKeyState(VK_F10) & 1) {
			espToggle();
		}
		else if (GetAsyncKeyState(VK_F11) & 1) {
			thermalToggle();
		}
		else if (GetAsyncKeyState(VK_F12) & 1) {
			// disable everything if even one is on
			if (isNoRecoilEnabled || isEspEnabled || isThermalEnabled) {
				noRecoilDisable();
				espDisable();
				thermalDisable();
			}
			else {
				// nothing is on, turn everything on
				noRecoilEnable();
				espEnable();
				thermalEnable();
			}
		}
		Sleep(100);
	}
	return 0;
}

//TESTED AND WORKS!!!
//need to unlock cheat/write protected now
void sendCommandToConsole(CHAR* szCommand)
{
	DWORD dwSend = (DWORD)0x413D60;

	//_asm push	szCommand
	//_asm push	0
	//_asm call	dwSend
	//_asm add	esp, 0x8

	_asm push	szCommand
	_asm push	1
	_asm push	1
	_asm call	dwSend
}

void WriteMemory(DWORD dwAddress, void* bytes, DWORD dwSize)
{
	DWORD flOldProtect = 0;
	VirtualProtect((void*)dwAddress, dwSize, PAGE_EXECUTE_READWRITE, &flOldProtect);
	memcpy((void*) dwAddress, bytes, dwSize);
    VirtualProtect((void*)dwAddress, dwSize, flOldProtect, &flOldProtect);
}

BOOL WINAPI DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpvReserved)
{
	if (dwReason == DLL_PROCESS_ATTACH) {
		DisableThreadLibraryCalls( hModule );
		CreateThread(0, 0, LoopFunction, 0, 0, 0);
	}
	return TRUE;
}